<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

use Kesillo\Core\Output;
use Kesillo\Core\View;
use Kesillo\Printing\Intermec;
//use Kesillo\Core\Router;


class KeDesk {
    
    public function __construct() 
    {
        // nothing for now, but this is app dependant.
    }
    
    public function main()
    {
        if (AppRouter === TRUE)
            $this->Route($_SERVER['REQUEST_URI']);
        else
        {
            $action = DefaultAction;
            $this->$action();
        }       
    }
    
    public function Route($uri = NULL)
    {
        $route = Kesillo\Core\Router::factory($uri);
        $map = $route->resolveRoute();
        
        
        if ($map['method'] == 'kesillo')
            $this->Index();
        else {
            if (method_exists($this, $map['method']))
                    $this->$map['method']();
        }
        
    }
    
    public function Index() 
    {
        //$p = Intermec::factory('pc4');
        //$p->Code39TpltShipA("123456789", "987654321", "XXX");
        $view = View::factory();
        $view->render('Default');
                
    }
    
    private function getPrinters()
    {
        $ptrs = Array('PC4-221','PC4-223','PC4-220','PC4-222','pc4');
        
        Output::KJson($ptrs);
    }
    
    private function listApps()
    {
        $t = Array(
            array(
                'appID' => 'ke-labeling',
                'appIcon' => 'glyphicon glyphicon-th-large',
                'name' => 'KeLabelling', 
                'desc' => 'Print the different kinds of predeffined labels'
            )
        );
        
        Output::KJson($t);
    }
 
    private function Debug()
    {
        echo "debug";
        $params = json_decode(file_get_contents('php://input'));
        print_r($params);
    }
    
    private function processLabelData()
    {
        $rawLabelPost = json_decode(file_get_contents('php://input'));
        
        if(count($rawLabelPost) > 1)
            $this->ProcessLabelList($rawLabelPost);
        else
            $this->ProcessSingleLabel($rawLabelPost);
        
    }
    
    private function ProcessSingleLabel($rawLabelPost = NULL)
    {
        $labelParams = Array();
        $label = NULL;
        $count = NULL;
        $token = NULL;
        $length = NULL;
        $printer = NULL;
        $type = NULL;
        
        $imecPrinters = Array(
            'PC4-220' => '\\\\housigma42.sigma.lan\PC4-220',
            'PC4-221' => '\\\\housigma43.sigma.lan\PC4-221',
            'PC4-222' => '\\\\housigma42.sigma.lan\PC4-222',
            'PC4-223' => '\\\\housigma43.sigma.lan\PC4-223',
            'pc4' => 'pc4'
        );

        foreach ($rawLabelPost as $key => $value) {
            if(preg_match('/printer/', $key) === 0) {
                $labelParams[substr($key, 2)] = $value;
                $type = substr($key, 0,2);
            }
            elseif(preg_match('/printer/', $key) === 1)
                $printer = $value;

        }

        if($type == 'sh') {
            foreach ($labelParams as $value) {
                $label[] = $value;
            }
            
            $this->printLabel($label, $printer, $type, $labelParams);
        }
        elseif($type !== 'sh') {
            foreach ($labelParams as $key => $value) {

                if($count === count($labelParams) - 1)
                    $token = '^M';
                else
                    $token = '^T';

                $label .= $value . $token;
                $count++;
            }
            
            for($i = 0; $i <= $labelParams['labelqty'] - 1; $i++) {
                $this->printLabel($label, $printer, $type, $labelParams);
            }
        }
        
    }

    private function ProcessLabelList($rawLabelPost = NULL)
    {
        $labelListParams = Array();
        $labelList = Array();
        $label = NULL;
        $count = NULL;
        $token = NULL;
        $length = NULL;
        $printer = NULL;
        $type = NULL;
        
        $imecPrinters = Array(
            'PC4-220' => '\\\\housigma42.sigma.lan\PC4-220',
            'PC4-221' => '\\\\housigma43.sigma.lan\PC4-221',
            'PC4-222' => '\\\\housigma42.sigma.lan\PC4-222',
            'PC4-223' => '\\\\housigma43.sigma.lan\PC4-223',
            'pc4' => 'pc4'
        );
        
        for($i = 0; $i <= count($rawLabelPost) - 1; $i++) {
            foreach ($rawLabelPost[$i] as $key => $value) {
                if(preg_match('/printer/', $key) === 0) {
                    $labelListParams[$i][substr($key, 2)] = $value;
                    $type = substr($key, 0,2);
                }
                elseif(preg_match('/printer/', $key) === 1)
                    $printer = $value;
                
            }
        }
        
        for($i = 0; $i <= count($labelListParams) - 1; $i++) {
            $count = 0;
            $label = NULL;
            
            foreach($labelListParams[$i] as $key => $value) {
                
                if($count === count($labelListParams[$i]) - 1)
                    $token = '^M';
                else
                    $token = '^T';

                $label .= $value . $token;
                $count++;
            }
            
            $labelList[] = $label;
        }
        
        for($i = 0; $i <= count($labelList) - 1; $i++) {
            for($j = 0; $j <= $labelListParams[$i]['labelqty'] - 1; $j++) {
                $this->printLabel($labelList[$i], $printer, $type, $labelListParams[$i]);
            }
        }
        
        
    }


    private function printLabel($label = NULL, $printer = NULL, $type = NULL, $labelParams = NULL)
    {
        $imec = Intermec::factory($printer);
        
        switch ($type) {
            case "sl":
                $imec->singleShipLabel($label, $labelParams);
                break;
            case "ll":
                $imec->singleShipLabel($label, $labelParams);
                break;
            case "sh":
                $imec->Code39TpltShipA($label[0], $label[1], strtoupper($label[2]));
                break;
            default:
                break;
        }
        
    }
}